﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Linq;
using WPFExcelReport.Helpers;
using System.Windows.Input;
using System.Windows.Controls;
namespace WPFExcelReport
{
    /// <summary>
    /// 专用于布卷剪布
    /// </summary>
    public partial class UIRQI03 : UIWBBST
    {
        #region ==IsCOnline===========
        public static readonly DependencyProperty IsCOnlineProperty = DependencyProperty.Register("IsCOnline",
            typeof(bool), typeof(UIRQI03),
            new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsCOnlineChanged));
        public bool IsCOnline
        {
            get
            {
                return (bool)GetValue(IsCOnlineProperty);
            }
            set
            {
                SetValue(IsCOnlineProperty, value);
                OnCOnlineChg(value);
            }
        }
        private static void OnIsCOnlineChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIRQI03;
            if (src != null)
            {
                src.IsCOnline = (bool)e.NewValue;
            }
        }
        public virtual void OnCOnlineChg(bool isonline)
        { }
        #endregion

        #region ==IsMachineReady===========
        public static readonly DependencyProperty IsMachineReadyProperty = DependencyProperty.Register("IsMachineReady",
        typeof(bool), typeof(UIRQI03),
        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsMachineReadyChanged));
        public bool IsMachineReady
        {
            get
            {
                return (bool)GetValue(IsMachineReadyProperty);
            }
            set
            {
                SetValue(IsMachineReadyProperty, value);
            }
        }
        private static void OnIsMachineReadyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIRQI03;
            if (src != null)
            {
                src.IsMachineReady = (bool)e.NewValue;
            }
        }
        #endregion
        #region ==CUSER===========
        public static readonly DependencyProperty CUSERProperty = DependencyProperty.Register("CUSER",
            typeof(string), typeof(UIRQI03),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCUSERChanged));
        public string CUSER
        {
            get
            {
                return (string)GetValue(CUSERProperty);
            }
            set
            {
                SetValue(CUSERProperty, value);
                if (MV == null)
                    return;
                TMV.CUSER = value;
            }
        }
        private static void OnCUSERChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIRQI03;
            if (src != null)
            {
                src.CUSER = (string)e.NewValue;
            }
        }
        #endregion 

        #region ==SHIFTPRODUCTION===========
        public static readonly DependencyProperty SHIFTPRODUCTIONProperty = DependencyProperty.Register("SHIFTPRODUCTION",
            typeof(string), typeof(UIRQI03),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSHIFTPRODUCTIONChanged));
        public string SHIFTPRODUCTION
        {
            get
            {
                return (string)GetValue(SHIFTPRODUCTIONProperty);
            }
            set
            {
                SetValue(SHIFTPRODUCTIONProperty, value);

                OnShifProdChg();
            }
        }
        private static void OnSHIFTPRODUCTIONChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIRQI03;
            if (src != null)
            {
                src.SHIFTPRODUCTION = (string)e.NewValue;
            }
        }
        public virtual void OnShifProdChg()
        { }
        #endregion

        TextBox cWB
        {
            get
            {
                return (TextBox)this.FindName("cWB");
            }
        }
        TWASTAMM cWS
        {
            get
            {
                return (TWASTAMM)this.FindName("cWS");
            }
        }
        TDRAUFTR cDR
        {
            get
            {
                return (TDRAUFTR)this.FindName("cDR");
            }
        }
        public UIRQI03()
        { 
             
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {

            base.Window_Loaded(sender, e);
            CUSER = app.User;
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                RWAUF = "";

            }
            else
            {
                RWAUF = TMV.WBBRWKRS + TMV.WBBRWLFD + TMV.WBBPOS;
            }
        }
        public virtual void Query()
        {
            if (IsEdit)
            {
                SaveCommand(null, null);
            }
            IsEdit = true;
            IsSaved = false;
            PM["STKNR"] = STKNR;
            PM["ART"] = ART;
            PM["ABAUF"] = ABAUF;
            PM["QITABLE"] = MACHINEID;
            SResult = TMV.QITXWBB(PM);
            MergeData(SResult, true);
            INTEXVALIDATE();
        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            base.QueryCommand(sender, e);
            Query();
        }

        public virtual void INTEXVALIDATE()
        {

        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            //TMV.UpdateWBBST(dwMaster, 0);
            PM["RWKRS"] = TMV.WBBRWKRS;
            PM["RWLFD"] = TMV.WBBRWLFD;
            PM["RWPOS"] = TMV.WBBPOS;
            TMV.RVDEFAULT(PM);
            base.SaveCommand(sender, e);
            IsEdit = true;
        }
        public override void Query_KeyUp(object sender, KeyEventArgs e)
        {
            base.Query_KeyUp(sender, e);
        }
        public void QIAMChg(object sender, RoutedEventArgs e)
        {
            IsMachineReady = !IsMachineReady;
        }
        public void NewWBCommand(object sender, RoutedEventArgs e)
        {
            SaveCommand(null, null);
            STKNR = "";
            cWB.Focus();
        }
        public override void OnSHIFTChg(string value)
        {
            if (dwMaster == null)
                return;
            dwMaster["WBBREFSCHL"] = SHIFT.PadRight(3);
            base.OnSHIFTChg(value);
        }
        //如果生产单对应的已经在系统内存在，则不在创建
        //否则创建DI单，表明品种总数量
        //创建PR单，表明织机上的卡 
        public void ChgQtyCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.WBBWMNRKRS = "M ";
            TMV.WBBWMLFDNR =Helper.Left( LOOMID.Trim().PadRight(4),4);
            TMV.UWBSSTKLG(dwMaster);
        }
       
        public override void OnCMTRChg(double cmtr)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            TMV.WBBISTKLG = cmtr;
            switch (TMV.WBBWAKZ)
            {
                case "R":
                    TMV.WBBSGEW = cmtr * cWS.RGRLFD * 0.001;
                    break;
                case "F":
                    TMV.WBBSGEW = cmtr * cWS.FGRLFD * 0.001;
                    break;
            }
            base.OnCMTRChg(cmtr);
        }
        public override void PrintCommand(object sender, RoutedEventArgs e)
        {
            bool IsFound = false;
            if (dwMaster == null)
                return;
            DataTable tb = TBMain.Clone();

            DataRow _dwWB = tb.Rows.Add();
            _dwWB.ItemArray = dwMaster.ItemArray;
            DataSet Set = new DataSet();
            Set.Merge(tb);
            switch (app.company)
            {
                default:
                case "JLT":
                    foreach (DataRow row in tb.Rows)
                    {
                        string PACKNR = row["WBBPACKNR"].ToString();
                        string art = row["WBBART"].ToString().Trim();
                        string dr = @"C:\Program Files\mabu\templates";
                        string filename = "JLB" + art.Substring(3, 4) + ".xls";
                        if (!System.IO.Directory.Exists(dr))
                        {
                            IsFound = false;
                        }
                        else
                        {
                            string[] files = System.IO.Directory.GetFiles(dr);
                            foreach (string file in files)
                            {
                                if (!file.Contains(filename)) continue; IsFound = true; cDR.ExTemplate = file; break;
                            }
                            if (!IsFound)
                            {
                                filename = "coillabel" + art.Substring(3, 4) + ".xls";
                                foreach (string file in files)
                                {
                                    if (!file.Contains(filename)) continue; IsFound = true; cDR.ExTemplate = file; break;
                                }
                            }
                        }
                    }
                    if (IsFound)
                        cDR.PRINT(Set);
                    break;
            }
        }
    }
}
